import Vue from 'vue'
import Vuex from 'vuex'
import userApi from '@/api/userApi.js'
 
import createPersistedState from 'vuex-persistedstate'
import user from './user.js'
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		user_id: uni.getStorageSync("user_id") || 0,
		userInfo: uni.getStorageSync("userInfo"),
		information: null,
		token: uni.getStorageSync('token') ||  null ,
		
	},
	modules: {
		user
	},
	mutations: {
		update(state, userInfo) {
			
			state.userInfo = userInfo;
		},
		tokenDate(state,token){
			state.token = token
		},
		logout(state) {
			state.hasLogin = false
			state.user_id = null
			state.userInfo = null
			state.token = null
			
			uni.clearStorageSync()
		}, 
	},
	actions: {
		logout({
			commit
		}, payLoad) {
			commit('logout','')
		},
		getUserInfos({commit, state},data){
			commit('tokenDate', data)
			console.log('个人')
			userApi.getUserInfo().then(res => {
				if(res.code == 1){
					if(res.data.phone == '') return
					commit('update', res.data)
					uni.$u.toast('登录成功')
					setTimeout(() => {
						uni.$u.route({
							url: "/pages/tabBarHome/home",
							type: "reLaunch"
						})
					}, 1000);
				}else{
					this.$toast('登录失败')
				}
			})
		}		 
	},
	plugins: [
		// veux持久化配置
		createPersistedState({
			storage: {
				getItem: (key) => uni.getStorageSync(key),
				setItem: (key, value) => uni.setStorageSync(key, value),
				removeItem: (key) => uni.removeStorageSync(key),
			}
		})
	]
})

export default store